
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <string.h>
#include <semaphore.h>
#include "hfile.h"

#define ACC_RIGHT 600
#define FILEPATH "/tmp/commchan.bin"
#define NUMINTS  (1000)
#define FILESIZE (NUMINTS * sizeof(int))

/*
 *This function will "listen" for data from the FSP for IPC, not data transmission
 */

//semaphore
sem_t *semUpEmpty;
sem_t *semUpFull;
sem_t *semDownFull;
sem_t *semDownEmpty;

int listenFsp(int descClient, char *dataBuffer)
{
    int fd;
    char *map;  /* mmapped array of chars(array of chars) */
    int true = 1;
    char buffer[2048];

    if((semUpFull = sem_open("semUpFull", 0)) == SEM_FAILED)
    {
        perror("Open semUpFull");
        if(sem_unlink("semUpFull") <0)
        {
            perror("blah");
        }
    }

    if((semUpEmpty = sem_open("semUpEmpty", 0)) == SEM_FAILED)
    {
        perror("Open semUpEmpty");
        if(sem_unlink("semUpEmpty") <0)
        {
            perror("semUpEmpty");
        }
    }

    if((semDownFull = sem_open("semDownFull", 0)) == SEM_FAILED)
    {
        perror("Open semDownFull");
        if(sem_unlink("semDownFull") <0)
        {
            perror("semDownFull");
        }
    }

    if((semDownEmpty = sem_open("semDownEmpty", 0)) == SEM_FAILED)
    {
        perror("Open semDownEmpty");
        if(sem_unlink("semDownEmpty") <0)
        {
            perror("semDownEmpty");
        }
    }
    printf("handled semaphores in listenfsp\n");

    fd = open(FILEPATH, O_RDWR);
    if (fd == -1)
    {
	perror("Error opening file for reading");
	exit(EXIT_FAILURE);
    }
    printf("opened filepath\n");

  
    map = (char*)mmap(0, FILESIZE,  PROT_WRITE, MAP_SHARED, fd, 0);
    if (map == MAP_FAILED) {
	close(fd);
	perror("Error mmapping the file");
	exit(EXIT_FAILURE);
    }
    printf("mapped\n");

    true = 1;   //Set true back on 1

    while(true == 1)    //loop listing for signal
    {
        printf("map content: %s\n", map);
        if(strcmp(map,"READY") == 0) //Compare  value of commchan mmap with READY, if true then call proceed with data transfer to upload mmap.
        {
            printf("strcmp succes\n");
            //Send READY to client
            if(send(descClient, "READY", 6)<0)
            {
                perror("send ready fail\n");
            }

                //while(dataBuffer != '\0') //Zolang er nog data is ontvang en verstuur
            while(1)
                {
                printf("descclient: %d\n", descClient);
                    //recive data form client, so that the data(dataBuffer) can be used in the sendToFsp
                    if((recv(descClient,buffer,2048, 0))<0)
                    {
                        perror("receive fail");
                        return 0;
                    }

                    if(sem_wait(semUpEmpty) < 0)
                    {
                        perror("wait semUpEmpty");
                    }
                    printf("databuffer: %s\n", buffer);
                    //Start sending
                    sendToFsp(buffer);  //Use dataBuffer and send the data to te upload mmap
                                            //Tested in prof of concept and is ok

                    if(sem_post(semUpFull) < 0)
                    {
                        perror("post semUpFull");
                    }
                    return 0;
                }
        }

        if(strcmp(map,"READYDOWNLOAD")) //Compare  value of commchan mmap with READYDOWNLOAD, if true then call proceed with data transfer to upload mmap.
        {
              if(sem_wait(semDownFull) < 0)
              {
                    perror("wait semDownFull");
              }

              recvFromFsp(descClient); //This function opens the mmap and sends the data to the client.

              if(sem_post(semUpEmpty) < 0)
              {
                   perror("post semDownEmpty");
              }
        }

         true = 0;   //Stop loop
    }

    printf("map: %s\n", map);//Debug for whats in the mmap


    return 0;
}


